package com.cssy.xxkj.basic.web;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.entity.BasicModeDeliveryDO;
import com.cssy.xxkj.basic.service.IBasicModeDeliveryTestService;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;

/**
 * 交货方式controller
 * @author zeyong
 *
 */
@Controller
@RequestMapping("/basic/delivery")
public class ModeDeliveryController extends BaseController<BasicModeDeliveryDO> {
	
	private static final String MANAGER = "/xxkj/basic/delivery/DeliveryManager";
	private static final String EIDT = "/xxkj/basic/delivery/DeliveryOperation";
	
	@Resource(name="basicModeDeliveryTestService")
	private IBasicModeDeliveryTestService basicModeDeliveryTestService;

	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		scs.setRemovedProperty(null);
		hander.getWheres(scs);
		SearchResult result = basicModeDeliveryTestService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}

	@Override
	public String operationForm(HttpServletRequest request, @PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		if(OperationFlag.ADD.equals(flag)){
			//新增
			model.addAttribute("flag", "add");
		}else{
			//修改
			BasicModeDeliveryDO basicModeDeliveryDO = basicModeDeliveryTestService.findListById(id);
			model.addAttribute("delivery",basicModeDeliveryDO);
			model.addAttribute("flag", "edit");
		}
		return EIDT;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			BasicModeDeliveryDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap=BodyMsg.getInstance();
		try {
			if(curAP==null||StringUtils.isBlank(curAP.getId())){
				//新增
				basicModeDeliveryTestService.save(curAP, SessionUtil.getUser(request));
			}else{
				basicModeDeliveryTestService.update(curAP, SessionUtil.getUser(request));
			}
			/*if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
				basicModePaymentTestService.update(curAP, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(curAP.getId());
			}else{//新增
				curAP.setZd001Code(syCodeService.getCode("FKMX","FKMX",SyCode.DATE_YYMM,5));
				try {
					basicModePaymentTestService.save(curAP, SessionUtil.getUser(request));
				} catch (Exception e) {
					e.printStackTrace();
					bodyMap.isFail();
					bodyMap.configWebInfor("数据有误，请检查后重新输入");
					return bodyMap.modelMap;
				}*/
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {			
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap=BodyMsg.getInstance();
		try{
			String isDelete = request.getParameter("isDelete");//标识是否是假删除
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				if(isDelete==null||"0".equals(isDelete)){
					basicModeDeliveryTestService.delete(field, SessionUtil.getUser(request));
				}else{
					basicModeDeliveryTestService.deleteBizRemoved(field);
				}
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
	}
}
